#!/usr/bin/perl -w

#
# FUNCIONES DE OBTENER DATOS SOBRE DOCUMENTOS (DESCRIPCIONES, PROPUESTAS)
#

require ("dbData.pl");


sub save_VersionVotation
{
    local ($dbName, $idDescription, $idVersion, $idTopic, $idAuthor, $gCont, $gImpro) = @_;
    local ($date);

    #Abrir la BdD
    $dbh  = &openDB($dbName);
    
    $sql = "SELECT * FROM Version WHERE deleteV='0' AND consolidated='0' AND idDescription=$idDescription;";
    $sth = $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";
    if ($row = $sth->fetchrow_hashref)
    {
      $dayV = $row->{dayV};
      ($date, $day3) = split (/ /,$dayV);
    }

    #Buscar votacion del author sobre la version
    $sql = "SELECT * FROM VoteVersion WHERE idDescription = $idDescription and idVersion = $idVersion and idAuthor = $idAuthor AND day>='$date';";
    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    if ($row = $sth->fetchrow_hashref)
    {
	$sql = "UPDATE VoteVersion SET degreeContinuity = $gCont, degreeImprovement = $gImpro, day = CURRENT_DATE, time = CURRENT_TIME WHERE idDescription = $idDescription and idVersion = $idVersion and idAuthor = $idAuthor;";
    }
    else
    {
	$sql = "INSERT INTO VoteVersion (idVersion, idDescription, idAuthor, idTopic, day, time, degreeContinuity, degreeImprovement) VALUES ($idVersion, $idDescription, $idAuthor, $idTopic, CURRENT_DATE, CURRENT_TIME, $gCont, $gImpro);";
    }

    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    $dbh->disconnect;    
}


sub get_activeVersion
{
    local ($dbName, $idDescription, $idAuthor) = @_;

     #Abrir la BdD
    $dbh  = &openDB($dbName);

    if ($idDescription == 0)
    {
	$sql = "Select * from Version where idAuthor = $idAuthor and consolidated = 0 and deleteV = 0;";
    }
    else
    {
	$sql = "Select * from Version where idDescription = $idDescription and consolidated = ";
	$sql = $sql . "0 and deleteV = 0;";
    }
    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    if ($idDescription != 0)
    {
	if($row = $sth->fetchrow_hashref)
	{
	    $data{idVersion}     = $row->{"idVersion"};
	    $data{idDescription} = $row->{"idDescription"};
	    $data{idAuthor}      = $row->{"idAuthor"};
	    $data{idTopic}       = $row->{"idTopic"};
	    $data{URLV}          = $row->{"URLV"};
	    $data{note}          = $row->{"note"};
	}    
	else
	{
	    $data{idVersion} = 0;
	}
    }
    else
    {
	$cont = 0;
	while($row = $sth->fetchrow_hashref)
	{
	    $cont++;
	    $data{idVersion.$cont}     = $row->{"idVersion"};
	    $data{idDescription.$cont} = $row->{"idDescription"};
	    $data{idAuthor.$cont}      = $row->{"idAuthor"};
	    $data{idTopic.$cont}       = $row->{"idTopic"};
	    $data{URLV.$cont}          = $row->{"URLV"};
	    $data{note}                = $row->{"note"};
	}
	$data{numRows} = $cont;
    }
    $dbh->disconnect;

    return (%data);
}


sub save_versionNote
{
    local ($dbName, $idVersion, $idDescription, $note) = @_;

     #Abrir la BdD
    $dbh  = &openDB($dbName);

    $sql = "UPDATE Version SET note = '$note' WHERE idVersion = $idVersion and idDescription = $idDescription;";

    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";


    $dbh->disconnect;

}


sub save_version
{
    local ($dbName, $idDescription, $idAuthor, $idTopic, $URLV, $titleDoc) = @_;
    
    #Abrir la BdD
    $dbh  = &openDB($dbName);

    $sql = "SELECT idVersion FROM Description WHERE idDescription = $idDescription;";

    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    if($row = $sth->fetchrow_hashref)
    {
	$idVersion = $row->{"idVersion"};
	$idVersion++;
    }

    
    $sql = "INSERT INTO Version (idVersion, idDescription, idTopic, idAuthor, URLV, dayV, timeV) VALUES ($idVersion, $idDescription, $idTopic, $idAuthor, '$URLV', CURRENT_DATE, CURRENT_TIME);";
    
    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    $sql = "UPDATE Description SET idea='$titleDoc' WHERE idDescription = $idDescription;";

    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    $dbh->disconnect;
    
    return ($idVersion);
}

sub is_localDescription
{
    local ($dbName, $idDescription) = @_;
    local ($directory, $inicia);
    #Abrir la BdD
    $dbh  = &openDB($dbName);

    $sql = "SELECT * FROM Description WHERE idDescription = $idDescription;";
    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    $is_local = 0;
    if($row = $sth->fetchrow_hashref)
    {
	$url = $row->{"URLD"};
	$n_votes = $row->{"totalVotes"};
	$n_votes = $n_votes + $row->{"lastVotes"};
	$fecha   = $row->{"dayD"};
	$idea = $row->{"idea"}; 
    }

    $sql = "SELECT * FROM LocalDescription WHERE idDescription = $idDescription;";
    
    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    if($row = $sth->fetchrow_hashref)
    {
	$directory   = $row->{"directory"};
	$inicialFile = $row->{"inicialFile"}; 
     }
    
    $dbh->disconnect;
    return ($directory, $inicialFile, $url, $n_votes, $fecha, $idea);
}

sub get_descriptionsByAuthor
{
  local ($dbName, $idAuthor, $idTopic) = @_;
  #Abrir la BdD
  $dbh  = &openDB($dbName);

  $sql = "SELECT * FROM Description WHERE idAuthor = $idAuthor and idTopic = $idTopic";
    
  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

 
  $numRows = 0;

  while ($row = $sth->fetchrow_hashref)
  {
      $numRows = $numRows + 1;
      $data{idDescription.$numRows} = $row->{idDescription};
      $data{dayD.$numRows}          = $row->{dayD};
      $data{URLD.$numRows}          = $row->{URLD};
      $data{lastVotes.$numRows}     = $row->{lastVotes};
      $data{totalVotes.$numRows}    = $row->{totalVotes};
  }

  $data{numRows} = $numRows;

  $dbh->disconnect;

  return %data;

}


sub get_descriptions
{
  local($dbName, $value) = @_;
  local(%data, $numRows, @field, $row);

  @field = &get_fields($dbName, "Description");
 
  #Abrir la BdD
  $dbh = &openDB($dbName);

  if ($deleteField eq '-')
  {
    $sql = "SELECT * FROM Description LEFT JOIN Author ON Description.idAuthor=Author.idAuthor WHERE idTopic=$value AND deleteD=0 ORDER BY valueCrys DESC, approvalVoting DESC , approvalVotes DESC, dayD DESC, idDescription DESC;";  
  }
  else
  {
    $sql = "SELECT * FROM Description LEFT JOIN Author ON Description.idAuthor=Author.idAuthor WHERE idTopic=$value AND deleteD= 0 ORDER BY valueCrys DESC, approvalVoting DESC , approvalVotes DESC, dayD DESC, idDescription DESC;";  
  }
 
  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";
 
  $numRows = 0;
  while ($row = $sth->fetchrow_hashref)
  {
    $numRows = $numRows + 1;
    foreach $i (0 .. $#field)
    {
      $data{$field[$i].$numRows} = $row->{$field[$i]};
    }
  }

  #$dbh->commit; #no usado por ahora porque solo es consulta
  $dbh->disconnect;

  $data{numRows} = $numRows;
  return %data;
}

sub get_descriptionsAlfabetic
{
  local($dbName, $value) = @_;
  local(%data, $numRows, @field, $row);

  @field = &get_fields($dbName, "Description");

  #Abrir la BdD
  $dbh = &openDB($dbName);

  if ($deleteField eq '-')
  {
    $sql = "SELECT * FROM Description LEFT JOIN Author ON Description.idAuthor=Author.idAuthor WHERE idTopic=$value AND deleteD=0 ORDER BY Author.nameAuthor ASC, dayD ASC;";  
  }
  else
  {
    $sql = "SELECT * FROM Description LEFT JOIN Author ON Description.idAuthor=Author.idAuthor WHERE idTopic=$value AND deleteD= 0 ORDER BY Author.nameAuthor ASC, dayD ASC;";  
  }
  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $numRows = 0;
  while ($row = $sth->fetchrow_hashref)
  {
    $numRows = $numRows + 1;
    foreach $i (0 .. $#field)
    {
      $data{$field[$i].$numRows} = $row->{$field[$i]};
    }
  }

  #$dbh->commit; #no usado por ahora porque solo es consulta
  $dbh->disconnect;

  $data{numRows} = $numRows;
  return %data;
}

sub get_descriptionsRandom
{
  local($dbName, $value) = @_;
  local(%data, $numRows, @field, $row);

  @field = &get_fields($dbName, "Description");

  #Abrir la BdD
  $dbh = &openDB($dbName);

  if ($deleteField eq '-')
  {
    $sql = "SELECT * FROM Description LEFT JOIN Author ON Description.idAuthor=Author.idAuthor WHERE idTopic=$value AND deleteD=0;";  
  }
  else
  {
    $sql = "SELECT * FROM Description LEFT JOIN Author ON Description.idAuthor=Author.idAuthor WHERE idTopic=$value AND deleteD= 0;";  
  }
  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $numRows = 0;
  while ($row = $sth->fetchrow_hashref)
  {
    $numRows = $numRows + 1;
    $taken[$numRows] = 0;
  }
  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";
  while ($row = $sth->fetchrow_hashref)
  {
    $num=int(rand($numRows))+1;
    while ($taken[$num] == 1)
    {
      $num=int(rand($numRows))+1;
    }
    $taken[$num] = 1;
    foreach $i (0 .. $#field)
    {
      $data{$field[$i].$num} = $row->{$field[$i]};
    }
  }

  #$dbh->commit; #no usado por ahora porque solo es consulta
  $dbh->disconnect;

  $data{numRows} = $numRows;
  return %data;
}

sub get_proposals
{
  local($dbName, $value, $date) = @_;
  local(%data, $numRows, @field, $row);
  #Modificado 25-11-2003 Incluir margen de bsqueda de proposiciones a mostrar.
  local (%data2, $propDays, $valueAux);
  %data2 = &get_data($dbName, "GeneralData", "idGeneralData", 1); 
  $propDays = $data2{propDays.1};
  $valueAux = &date2value($date);
  $valueAux = $valueAux - $propDays;
  $date = &value2date($valueAux);
  
  $deleteField = &get_deleteField($dbName, "Proposal");
  
  
  @field = &get_fields($dbName, "Proposal");

  #Abrir la BdD
  $dbh = &openDB($dbName);

  if ($deleteField eq '-')
  {
    $sql = "SELECT * FROM Proposal LEFT JOIN Author ON Proposal.idAuthor=Author.idAuthor WHERE idTopic=$value AND dayEnd>='$date' AND associateDes=0 ORDER BY dayIni ASC;";  
  }
  else
  {
    $sql = "SELECT * FROM Proposal LEFT JOIN Author ON Proposal.idAuthor=Author.idAuthor WHERE idTopic=$value AND dayEnd>='$date' AND $deleteField = 0 AND associateDes=0 ORDER BY dayIni ASC;";  
  }

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $numRows = 0;
  while ($row = $sth->fetchrow_hashref)
  {
    $numRows = $numRows + 1;
    foreach $i (0 .. $#field)
    {
      $data{$field[$i].$numRows} = $row->{$field[$i]};
    }
  }

  #$dbh->commit; #no usado por ahora porque solo es consulta
  $dbh->disconnect;

  $data{numRows} = $numRows;
  return %data;
}

sub get_proposal
{
  local($dbName, $idAuthor, $idTopic, $date, $idDescription) = @_;
  local(%data, $numRows, @field, $row);

  #Abrir la BdD
  $dbh = &openDB($dbName);

  $sql = "SELECT * FROM Proposal WHERE idAuthor=$idAuthor AND idTopic=$idTopic AND dayEnd>='$date' AND associateDes=0 AND deleteP=0 ORDER BY dayEnd;";

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  if ($row = $sth->fetchrow_hashref)
  {
    $thisDate = $row->{dayEnd};
    ($thisDate, $nulo) = split (/ /,$thisDate);
    ($day, $month, $year) = split (/-/,$thisDate);
    $date = $year . "-" . $month . "-" . $day;

    $sql = "UPDATE Proposal SET associateDes=$idDescription WHERE associateDes=0 AND idAuthor=$idAuthor AND idTopic=$idTopic AND deleteP=0 AND dayEnd='$thisDate';";

    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    $dev = 1;
  }
  else
  {
    $dev = 0;
  }

  #$dbh->commit; #no usado por ahora porque solo es consulta
  $dbh->disconnect;

  return $dev;
}


sub insert_description
{
  local ($dbName, $idTopic, $URL, $idAuthor, $directory, $inicialFile, $titleDoc) = @_;
  local ($idF, %data);

  #Abrir la BdD
  $dbh = &openDB($dbName);

  $sql = "INSERT INTO Description (idTopic, URLD, idAuthor, dayD, timeD, idea) VALUES ($idTopic, '$URL', $idAuthor, CURRENT_DATE, CURRENT_TIME, '$titleDoc');";

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  #Se obtiene el identificador de la descripcion
  $sql = "SELECT * FROM Description WHERE idTopic=$idTopic AND idAuthor=$idAuthor AND URLD='$URL' ORDER BY idDescription DESC;";

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  if ($row = $sth->fetchrow_hashref)
  {
    $idF = $row->{idDescription};
  }

   $sql = "INSERT INTO LocalDescription (idDescription, directory, inicialFile) values ($idF, '$directory', '$inicialFile');";
 
  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $dbh->disconnect;  
  return $idF;
}

sub insert_proposal
{
  local ($dbName, $idTopic, $idAuthor, $dateEnd, $comment) = @_;
  local ($idF, %data);

  #Abrir la BdD
  $dbh = &openDB($dbName);
  
  #Obtener el campo Proposal.deleteP
  $deleteField = &get_deleteField($dbName, "Proposal");
  
  #Si no tiene campo de delete...
  if ($deleteField eq '-')
  {
	#Primero ver que no hay ya una propuesta de este autor en este tema
	$sql = "SELECT * FROM Proposal WHERE idTopic=$idTopic AND idAuthor=$idAuthor AND associateDes=0;";
  }
  else
  {
	#Primero ver que no hay ya una propuesta de este autor en este tema
	$sql = "SELECT * FROM Proposal WHERE idTopic=$idTopic AND idAuthor=$idAuthor AND $deleteField = 0 AND associateDes=0;";
  }
  

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";
  if ($row = $sth->fetchrow_hashref)
  {
    return -1;
  }

  if ($comment eq '')
  {
    $sql = "INSERT INTO Proposal (idTopic, idAuthor, dayIni, timeIni, dayEnd) VALUES ($idTopic, $idAuthor, CURRENT_DATE, CURRENT_TIME, '$dateEnd');";
  }
  else
  {
    $sql = "INSERT INTO Proposal (idTopic, idAuthor, dayIni, timeIni, dayEnd, comment) VALUES ($idTopic, $idAuthor, CURRENT_DATE, CURRENT_TIME, '$dateEnd', '$comment');";
  }

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $sql = "SELECT idProposal FROM Proposal WHERE idAuthor=$idAuthor AND $idTopic=$idTopic ORDER BY idProposal DESC;";

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  if ($row = $sth->fetchrow_hashref)
  {
    $idP = $row->{idProposal};
  }

  $dbh->disconnect;

  return $idP;
}

sub update_description
{
    local ($dbName, $idDescription, $url, $directory, $inicialFile, $titleDoc) = @_;
    

    #Abrir la BdD
    $dbh = &openDB($dbName);


    $sql = "INSERT INTO UpdateDescription (operation, idDescription, idea, idTopic, URLD, idAuthor, idVersionOld, idVersionNew, valueCrys, valueCrys1, valueCrys2, lastVotes, lastVoting, totalVotes, totalVoting, totalVotesP, totalVotingP, approvalVotes, approvalVoting, approvalVotesP, approvalVotingP, approvalAccess, votesAccess, crystallised, day, today) SELECT 0, idDescription, idea, idTopic, URLD, idAuthor, idVersion, 0, valueCrys, valueCrys1, valueCrys2, lastVotes, lastVoting, totalVotes, totalVoting, totalVotesP, totalVotingP, approvalVotes, approvalVoting, approvalVotesP, approvalVotingP, approvalAccess, votesAccess, crystallised, dayD, CURRENT_DATE FROM Description WHERE idDescription = $idDescription;";

    $sth =  $dbh->prepare($sql);

    $sth->execute || die "No se puede ejecutar la orden SQL";

  
    #Se obtiene el identificador de la descripcion
    $sql = "UPDATE Description SET URLD = '$url', idea='$titleDoc' WHERE idDescription = $idDescription;";
#print " sql $sql";    
    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";
 
    $sql = "SELECT * from LocalDescription where idDescription = $idDescription";

    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    if ($row = $sth->fetchrow_hashref)
    {
	$sql = "UPDATE LocalDescription SET directory = '$directory', inicialFile = '$inicialFile' where idDescription = $idDescription";
    }
    else
    {
	$sql = "INSERT INTO LocalDescription (directory, inicialFile, idDescription) VALUES ('$directory', '$inciialFile', $idDescription)";
    }
    
    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    $dbh->disconnect;  
    
}

sub delete_description
{
  local ($dbName, $idTopic, $URL, $idAuthor) = @_;
  local ($idF, %data);

  #Abrir la BdD
  $dbh = &openDB($dbName);

  #Se obtiene el identificador de la descripcion
  $sql = "SELECT * FROM Description WHERE idTopic=$idTopic AND idAuthor=$idAuthor AND URLD='$URL' AND deleteD=0 ORDER BY idDescription DESC;";

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  if ($row = $sth->fetchrow_hashref)
  {
    $idF = $row->{idDescription};

    #Se obtiene el identificador de la descripcion
    $sql = "UPDATE Description SET deleteD=1 WHERE idDescription=$idF ;";

    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";

    $dbh->disconnect;  
    return $idF;
  }
}

sub get_crysDescriptions
{
  local($dbName, $idTopic, $idAuthor) = @_;
  local(%data, $numRows, @field, $row);

  @field = &get_fields($dbName, "Description");

  #Abrir la BdD
  $dbh = &openDB($dbName);

  if ($deleteField eq '-')
  {
    $sql = "SELECT * FROM Description LEFT JOIN Author ON Description.idAuthor=Author.idAuthor WHERE Description.idAuthor=$idAuthor AND idTopic=$idTopic AND deleteD=0 AND crystallised='1' ORDER BY valueCrys DESC, approvalVoting DESC , dayD ASC;";  
  }
  else
  {
    $sql = "SELECT * FROM Description LEFT JOIN Author ON Description.idAuthor=Author.idAuthor WHERE Description.idAuthor=$idAuthor AND idTopic=$idTopic AND deleteD=0 AND crystallised='1' ORDER BY valueCrys DESC, approvalVoting DESC , dayD ASC;";  
  }

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $numRows = 0;
  while ($row = $sth->fetchrow_hashref)
  {
    $numRows = $numRows + 1;
    foreach $i (0 .. $#field)
    {
      $data{$field[$i].$numRows} = $row->{$field[$i]};
    }
  }

  #$dbh->commit; #no usado por ahora porque solo es consulta
  $dbh->disconnect;

  $data{numRows} = $numRows;
  return %data;
}

sub put_access 
{
  local($dbName, $idDes) = @_;    
  local (%data);

  #Abrir la BdD
  $dbh = &openDB($dbName);

  $sql = "UPDATE Description SET totalAccess = totalAccess + 1 WHERE idDescription = $idDes;";

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";


  $sql = "SELECT idTopic FROM Description WHERE idDescription = $idDes";
  
  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  if ($row = $sth->fetchrow_hashref)
  {
      $idTopic = $row->{idTopic};
  }
  
  %data = &get_data($dbName, "Description", "idDescription", $idDes); 
  $idTopic = $data{idTopic.1};

  $sql = "INSERT INTO Access (idDescription, idTopic, dayAc, timeAc) values ($idDes, $idTopic, CURRENT_DATE, CURRENT_TIME);";

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $dbh->disconnect;
  
  return 1;
}

sub insert_file
{
  local ($dbName, $idUser, $URLinHD, $URLforWeb, $upd) = @_;

  #Abrir la BdD
  $dbh = &openDB($dbName);

  $sql = "INSERT INTO Upload (idAuthor, URLinHD, URLforWeb, day, time, upd) VALUES ($idUser, $URLinHD, $URLforWeb, CURRENT_DATE, CURRENT_TIME, $upd);";

  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $dbh->disconnect;
  
  return 1;
}

sub change_numVotes
{
  local ($dbName, $idAuthor, $idDes, $value, $LimitVotes) = @_;

  #Abrir la BdD
  $dbh = &openDB($dbName);

  #Se restan el nmero de votos empleados si hay l�ites en el uso de votos
  if ($LimitVotes eq '1')
  {
    $sql = "UPDATE Author SET numVotes = numVotes - 1 WHERE idAuthor = $idAuthor;";
    $sth =  $dbh->prepare($sql);
    $sth->execute || die "No se puede ejecutar la orden SQL";
  } 
 
  $sql = "UPDATE Description SET lastVoting = lastVoting + $value WHERE idDescription = $idDes;";
  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $sql = "UPDATE Description SET lastVotes = lastVotes + 1 WHERE idDescription = $idDes;";
  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $sql = "INSERT INTO AuthorVotes (idAuthor, idDescription, day, time, val) VALUES ($idAuthor, $idDes, CURRENT_DATE, CURRENT_TIME, $value)";
  $sth =  $dbh->prepare($sql);
  $sth->execute || die "No se puede ejecutar la orden SQL";

  $dbh->disconnect;
  return 1;  
}


return 1;
